var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6036265-4']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
			
function hidePopup(callback) {
	$("#fade").fadeOut(200);
	$("#popup").fadeOut(200);
	if(callback != null) {
		callback();
	}
}

function showPopup(callback) {
	$("#fade").fadeIn(200);
	$("#popup").fadeIn(200);
	if(callback != null) {
		callback();
	}
}

function redirect(link) {
	window.location = link;
}

$.fn.handleLabel = function() { 
	var span = $('#label-'+$(this).attr('id'));
	if(span.length > 0) {
		if($(this).val()) {
			span.css('visibility', 'hidden');
			$(this).attr('title',span.html());
		} else {
			span.css('color','#BBBBBB');
			span.css('visibility', '');
			$(this).attr('title','');
		}
	}
};

$.fn.showLabel = function() {
	var span = $('span#label-'+$(this).attr('id'));
	if(span.length > 0) {
		span.css('color','#666666');
		span.css('visibility', '');
		$(this).attr('title','');
	}
};

var fixLabels = function() {
	var inputs = $('form input, form textarea');
	$.each(inputs, function(k,v) {
		var elem = $(v);
		var label = $('label[for="'+elem.attr('id')+'"]');
		var span = $("<span id='label-"+elem.attr('id')+"' class='label'>"+label.html()+"</span>");
		if(label.attr('class').indexOf('required') > -1) {
			span.addClass('required');
		}
		span.addClass(label.attr('class'));
		span.insertBefore(elem);
		label.remove();
		if(elem.val() != '') {
			span.addClass('none');
		}
	});
	$('span.label').click(function() {
		var id = $(this).attr('id').split('-');
		$("#"+id[1]).focus();
		$(this).css('color','#BBBBBB');
		$(this).css('visibility','');
	});
	
    $('input, textarea').live('focusin keyup change', function() {$(this).handleLabel();});
    $('input, textarea').live('focusout', function() {
    	if($(this).val() == '') {
    		$(this).showLabel();
    	}
    });
};

$(document).ready(function() {
	fixLabels();
});